import menu from '@/api/menu'
export default{
    data(){
        return {
            menus:[],
            defaultProps: {
                children: 'menus',
                label: 'cdmc',
                isLeaf: 'leaf'
            },
            currentNode:null,
            parentNode:null
        }
    },
    methods:{
        onTreeNodeClick(data,node,tree){

            //console.log(node)
            this.currentNode = node;
            this.parentNode = node.parent;
            this.setFormValue(node);
        },
        loadMenuTree(){
            menu.queryMenu()
            .then(response => {
                this.loading = false;
                const { data } = response;
                //console.log(data)
                if( data.error_code == 0 ){
                    
                    this.menus = data.menus;
                    //this.$store.commit('app/setAuthInfo',data);
                }else{
                    //this.showError('获取授权信息失败！');
                }
            }).catch(error => {
                this.loading = false;
                //this.showError('网络错误，获取授权信息失败！');
            });
        },
        getNodeIcon(node){

            if( node.isLeaf ){
                if( node.data.icon ){
                    return <i class={node.data.icon}></i>
                }
                else{
                    return <i class="rs-icon-file-fill0"></i>
                }
            }else{
                if( node.expanded ){
                    return [<i class="rs-icon-folder-open-fill"></i>,<i class={node.data.icon}></i>]
                }else{
                    return [<i class="rs-icon-folder-fill"></i>,<i class={node.data.icon}></i>]
                }
            }
        },
        renderContent(h, { node, data, store }) {

            //console.log(arguments)
            return (
            <div class={{"rs-tree-node":true,"leaf":node.isLeaf}}>
                {this.getNodeIcon(node)}
                <span class="label">{node.label}</span>
            </div>);
        },
        setFormValue(node){
            //console.log(node);
            const menu = node.data;
            const {form} = this;
            if( menu.cdbh == 'root' ){
                form.pcdmc = '';
                form.pcdbh = '';
                form.pcdlj = '';
            }else{
                const parentMenu = node.parent.data;
                form.pcdbh = parentMenu.cdbh;
                form.pcdmc = parentMenu.cdmc;
                form.pcdlj = parentMenu.cdlj;
            }
            form.cdbh = menu.cdbh;
            form.cdmc = menu.cdmc;
            form.cdlx = menu.cdlx;
            form.cdxh = menu.cdxh;
            form.cdlj = menu.cdlj;
            form.icon = menu.icon;
            form.sfhc = menu.sfhc;
        },
        initCurrentNode(){
            const menuTree = this.$refs.menuTree;
            const that = this;
            let count = 0;
            const timer = setInterval(()=>{
                
                const node = menuTree.getNode({cdbh:'root'});
                if( node ){
                        clearInterval(timer);
                        that.currentNode = node;
                        that.parentNode = node.parent;
                        that.setFormValue(node);
                }else{
                    if( count >= 10 ){
                        clearInterval(timer);
                        this.showError("初始化错误！");
                    }
                }
                count ++;
            },300)
        },
        insertNodeData(node,data){

            const menuTree = this.$refs.menuTree;
            const childNodes = node.childNodes;
            if( childNodes.length == 0 ){
                menuTree.append(data,this.currentNode);
            }else{
                
                let lasNode = null;
                let refNode = null;
                for(let i = childNodes.length -1; i >=0; i -- ){

                    lasNode = childNodes[i];
                    if( lasNode.data.cdxh <= data.cdxh ){
                        refNode = lasNode;
                        break;
                    }
                }
                if( refNode ){
                    menuTree.insertAfter(data,refNode);
                }else{
                    menuTree.insertBefore(data,lasNode);
                }                
            }
            this.$nextTick(()=>{
                menuTree.setCurrentKey(data.cdbh);
                this.currentNode = menuTree.getNode(data.cdbh);
                this.setFormValue(this.currentNode);
                const parent = this.currentNode.parent;
                if( !parent.expanded ){
                    parent.expand();
                }
            })
        },
        saveNodeData(node,data){

            const menuTree = this.$refs.menuTree;
            const currentData = node.data;
            const childNodes = node.parent.childNodes;

            if( data.cdxh != currentData.cdxh && childNodes.length > 1){

                const parent = node.parent;
                menuTree.remove(data);
                this.insertNodeData(parent,data);
            }else{
                for(let key in currentData ){

                    if( data[key] && currentData[key] !== data[key] ){
    
                        this.$set(currentData,key,data[key])
                    }
                }
            }
        },
        deleteNodeData(node){

            const menuTree = this.$refs.menuTree;
            if(node.data.cdbh === 'root'){
                for(let i = node.childNodes.length; i >=0; i -- ){
                    menuTree.remove(node.childNodes[i]);
                }
                menuTree.setCurrentKey(node.data.cdbh);
                this.currentNode = menuTree.getNode(node.data.cdbh);
            }else{
                const parent = node.parent;
                menuTree.remove(node);
                menuTree.setCurrentKey(parent.data.cdbh);
                this.currentNode = menuTree.getNode(parent.data.cdbh);
            }
            this.setFormValue(this.currentNode);
        }
    }
}